package cn.edu.pzhu.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

import com.alibaba.fastjson.JSON;
public class FileUtil {
	private static String path = "d:/upload/";
	private static double size = 100 * 1024 * 1024; //100M

	public static void setPath(String path) {
		FileUtil.path = path;
	}

	public static void setSize(double size) {
		FileUtil.size = size;
	}
	public static boolean deleteFile(String filepath) {
		java.io.File file = new java.io.File(path + filepath);
		try {
			if (file.exists()) {
				file.delete();
			}
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	public static Result uploadFile(Part filePart) {
		if (filePart==null || filePart.getSize()==0 || filePart.getSubmittedFileName()==null || filePart.getSubmittedFileName().equals("")) {
			return new Result(false,"","文件信息不完整");
		}
		try {
			// "文件大小不能超过100MB"
			if (filePart.getSize() > size) {
				filePart.delete();
				return new Result(false,"","文件大小不能超过100MB");
			}
			String oldName = filePart.getSubmittedFileName();
			String newName = UUID.randomUUID().toString() + oldName.substring(oldName.lastIndexOf("."));
			filePart.write(path + newName);
			System.out.println("文件保存至：" + path + newName);
			return new Result(true,newName,"文件上传成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false,"","文件上传失败");
		}
	}

	public static Result uploadFiles(Part[] files) {
		int count = 0;
		
		try {
			List<String> list = new ArrayList<>();
			for (int i = 0; i < files.length; i++) {
				Part filePart = files[i];
				if (filePart.getSize() > size) {
					filePart.delete();
					list.add("文件大小超过100MB");
				}
				String oldName = filePart.getSubmittedFileName();
				String newName = UUID.randomUUID().toString() + oldName.substring(oldName.lastIndexOf("."));
				filePart.write(path + newName);
				list.add(newName);
				count++;
			}
			return new Result(true, JSON.toJSONString(list),count + "个文件上传成功!");
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false,"",(files.length - count) + "个文件上传失败!");
		}
	}

	/**
	 * 
	 * @param fileName 文件名
	 * @param filepath 文件路径	
	 * @param response HttpServletResponse
	 * @return 
	 */
	public static Result downloadFile(String fileName, String filepath, HttpServletResponse response) {
		try {
			File file = new File(path + filepath);
			System.out.println(path+filepath);
			if (!file.exists()) {
				return new Result(false,"","资源已丢失或不可用!");
			}
			response.reset();
			response.setCharacterEncoding("UTF-8");
			fileName = URLEncoder.encode(fileName, "UTF-8") + filepath.substring(filepath.lastIndexOf("."));
			response.setHeader("Content-Disposition", "attachment;fileName=" + fileName);
			InputStream input = new FileInputStream(file);
			ServletOutputStream out = response.getOutputStream();
			byte[] buff = new byte[1024];
			int len;
			while ((len = input.read(buff)) != -1) {
				out.write(buff, 0, len);
				out.flush();
			}
			input.close();
			out.close();			
			return new Result(true,"文件正常下载完毕","文件下载成功");
		} catch (Exception e) {
			return new Result(true,"下载过程被用户中断","文件下载失败!");
		}

	}
}